﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MovieRental.XmlDataService;
using System.ComponentModel;

namespace MovieRental.Entities
{
    [DataContract]
    public abstract class BaseEntity<TKey> : INotifyPropertyChanged
    {
        [XmlPrimaryKey]
        [DataMember]
        public TKey Id { set; get; }

        public override string ToString()
        {
            return string.Format("{0}:{1}", GetType(), Id); 
        }

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void SetProperty<T>(ref T field, T value, string name)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                OnPropertyChanged(name);
            }
        }

    }
}
